package com.example.demosession.session;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
@EnableRedisHttpSession
public class Config {

    /**
     * jedis简单配置
     *
     * @return
     */
    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPort(6379);
        connectionFactory.setHostName("127.0.0.1");
        return connectionFactory;
    }

    /**
     * CookieHttpSessionStrategy 配置
     *
     * @return
     */
    @Bean
    public CookieHttpSessionStrategy cookieHttpSessionStrategy() {
        DefaultCookieSerializer cookiesSerializer = new DefaultCookieSerializer();
        cookiesSerializer.setDomainName("127.0.0.1");
//        cookiesSerializer.setCookiePath("/");
        CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy();
        cookieHttpSessionStrategy.setCookieSerializer(cookiesSerializer);
        return cookieHttpSessionStrategy;
    }
}